package com.nian.db.hive.controller;

import com.nian.common.model.ServerResponse;
import com.nian.db.hive.model.StudentModel;
import com.nian.db.hive.service.IHiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用途：
 * 开发：zhangmj
 * 日期：2020/4/25 0025 18:46
 */
@RestController
@RequestMapping("test/hive")
public class TestHiveController {

    @Autowired
    private IHiveService iHiveService;

    /**
     * 根据表面查询全部接口
     * @param tableName
     * @return
     */
    @PostMapping("listAll/{tableName}")
    public ServerResponse listAll(@PathVariable("tableName") String tableName){
        List list = iHiveService.listAll(tableName);
        return ServerResponse.createBySuccess(list);
    }

    /**
     * 样例接口：根据表面查询全部数据
     * @return
     */
    @PostMapping("exampleQuery")
    public ServerResponse simpleQuery(){
        String tableName = "student";
        List list = iHiveService.listAll(tableName);
        return ServerResponse.createBySuccess(list);
    }

    /**
     * 样例接口：创建表
     * @return
     */
    @PostMapping("exampleCreateTable")
    public ServerResponse exampleCreateTable(){
        iHiveService.execute(StudentModel.getInitSql());
        return ServerResponse.createBySuccess();
    }

    /**
     * filePath 是在 hive 服务端的文件目录
     * @param tableName
     * @param pathFile
     * @return
     */
    @PostMapping("loadLocalData")
    public ServerResponse loadLocalData(String tableName, String pathFile){
        iHiveService.loadLocalData(pathFile, tableName);
        return ServerResponse.createBySuccess();
    }

    /**
     * 初始化样例
     * @return
     */
    @PostMapping("initExample")
    public ServerResponse initExample(){
        // 1 创建表
        String tableName = "member";
        StringBuffer createTableSql = new StringBuffer("drop table if exists  ");
        createTableSql.append(tableName);
        iHiveService.execute(createTableSql.toString());
        createTableSql.setLength(0);
        createTableSql.append(" create table ").append(tableName);
        createTableSql.append(" (id bigint comment '主键' ,");
        createTableSql.append(" name string  comment '姓名' ,");
        createTableSql.append(" address string comment '地址'," );
        createTableSql.append(" en_name string comment '英文名'," );
        createTableSql.append(" member_family int comment '家庭成员数量'," );
        createTableSql.append(" old_time string comment '数据时间')" );
        createTableSql.append(" row format delimited fields terminated by '\t'");
        createTableSql.append(" stored as textfile");
        iHiveService.execute(createTableSql.toString());
        // 2 新增数据, filePath 是在 hive 服务端的文件目录
        String filePath = "/opt/member.txt";
        iHiveService.loadLocalData(filePath, tableName);
        String querySql = "select * from member";
        List list = iHiveService.executeQuery(querySql);
        return ServerResponse.createBySuccess(list);
    }
}
